<template>
	<view calss="container">
		<mescroll-uni :down="downOption" :up="upOption" @down="downCallback">
			<view class="my-header">
				<image :src="my_info.status==1&&my_info.image_url?my_info.image_url:image_url"></image>
				<text class="name" v-if="my_info.status==1">{{my_info.nick_name?my_info.nick_name:'未设置'}}</text>
				<view v-else>
					<!-- #ifdef MP-WEIXIN -->
					<button hover-class="none" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wx_login" class="login">登录</button>
					<!-- #endif -->
					<!-- #ifdef APP-PLUS -->
					<button hover-class="none" @tap="wx_login" class="login">登录</button>
					<!-- #endif -->
					<!-- #ifdef H5 -->
					<button hover-class="none" @tap="wx_login" class="login">登录</button>
					<!-- #endif -->
				</view>
				<tui-icon name="arrowright" color="#FFFFFF" class="tui-badge" :size="28" @tap="setting"></tui-icon>
			</view>
			<tui-list-view title="我的订单" class="tui-list-view">
				<view class="tui-menu-itembox">
					<view class="tui-menu-item" @tap="order" data-index="1">
						<view class="tui-badge-box">
							<tui-icon name="shop-fill" color="#ff7900" :size="28"></tui-icon>
							<tui-badge type="red" tui-badge-class="tui-menu-badge" size="small" v-if="my_info.order_status1>0">{{my_info.order_status1>99?99:my_info.order_status1}}</tui-badge>
						</view>
						<view class="tui-menu-text">待付款</view>
					</view>
					<view class="tui-menu-item"  @tap="order" data-index="2">
						<view class="tui-badge-box">
							<tui-icon name="strategy" color="#ff7900" :size="28"></tui-icon>
							<tui-badge type="red" tui-badge-class="tui-menu-badge" size="small" v-if="my_info.order_status2>0">{{my_info.order_status2>99?99:my_info.order_status2}}</tui-badge>
						</view>
						<view class="tui-menu-text">待发货</view>
					</view>
					<view class="tui-menu-item"  @tap="order" data-index="3">
						<view class="tui-badge-box">
							<tui-icon name="transport-fill" color="#ff7900" :size="28"></tui-icon>
							<tui-badge type="red" tui-badge-class="tui-menu-badge" size="small" v-if="my_info.order_status3>0">{{my_info.order_status3>99?99:my_info.order_status3}}</tui-badge>
						</view>
						<view class="tui-menu-text">待收货</view>
					</view>
					<view class="tui-menu-item"  @tap="order" data-index="4">
						<view class="tui-badge-box">
							<tui-icon name="message-fill" color="#ff7900" :size="28"></tui-icon>
							<tui-badge type="red" tui-badge-class="tui-menu-badge" size="small" v-if="my_info.order_status4>0">{{my_info.order_status4>99?99:my_info.order_status4}}</tui-badge>
						</view>
						<view class="tui-menu-text">待评价</view>
					</view>
					<view class="tui-menu-item"  @tap="order" data-index="5">
						<view class="tui-badge-box">
							<tui-icon name="redpacket-fill" color="#ff7900" :size="28"></tui-icon>
							<tui-badge type="red" tui-badge-class="tui-menu-badge" size="small" v-if="my_info.order_status5>0">{{my_info.order_status5>99?99:my_info.order_status5}}</tui-badge>
						</view>
						<view class="tui-menu-text">退款/售后</view>
					</view>
				</view>
			</tui-list-view>
			<tui-list-view class="tui-list-view">
				<tui-list-cell @click="recharge_list" :arrow="true" class="tui-list">
					<tui-icon name="wealth-fill" :size="24" color="#ff7900"></tui-icon>
					<view class="tui-list-cell-name">我的钱包</view>
					<view class="tui-right">{{my_info.amount}}</view>
				</tui-list-cell>
				<tui-list-cell @click="site_list" :arrow="true" class="tui-list">
					<tui-icon name="position-fill" :size="24" color="#ff7900"></tui-icon>
					<view class="tui-list-cell-name">地址管理</view>
				</tui-list-cell>
				<tui-list-cell @click="my_collect" :arrow="true" class="tui-list">
					<tui-icon name="like" :size="24" color="#ff7900"></tui-icon>
					<view class="tui-list-cell-name">我的收藏</view>
					<tui-badge type="danger" class="tui-badge" v-if="my_info.collect_count>0">{{my_info.collect_count>99?'99+':my_info.collect_count}}</tui-badge>
				</tui-list-cell>
				<tui-list-cell @click="config_content" :arrow="true" class="tui-list">
					<tui-icon name="home-fill" :size="24" color="#1afa29"></tui-icon>
					<view class="tui-list-cell-name">关于我们</view> 
				</tui-list-cell>
				<tui-list-cell @click="login" :arrow="true" class="tui-list" v-if="my_info.status!=1">
					<tui-icon name="friendadd-fill" :size="24" color="#1afa29"></tui-icon>
					<view class="tui-list-cell-name">手机登录</view> 
				</tui-list-cell>
			</tui-list-view>
		</mescroll-uni>	
	</view>
</template>
<script>
	import tuiListView from "@/components/list-view/list-view"
	import tuiListCell from "@/components/list-cell/list-cell"
	import tuiIcon from "@/components/icon/icon"
	import tuiBadge from "@/components/badge/badge"
	import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue";
	
	import My from './my-model.js'   
	var my = new My(); 
	export default {
		components: {
			tuiListView,
			tuiListCell,
			tuiIcon,
			tuiBadge,
			MescrollUni
		},
		data() {
			return {
				downOption: {
					auto: false
				},
				upOption: {
						use: false, // 是否启用上拉加载; 默认true
						auto: false, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
				},		
				my_info:{},//用户信息
				image_url:'../../static/images/my/mine_def_touxiang_3x.png',//默认头像
				// #ifdef MP-WEIXIN
				provider:1,	
				//#endif
				// #ifdef H5
				provider:3,	
				//#endif
				// #ifdef APP-PLUS
				provider:4,	
				//#endif
			}
		},
		onLoad: function(options) {
			var that = this;
			// #ifdef H5
			if(options.openid){
				var data = {
					gzh_open_id:options.openid,
					nick_name:options.nickname,
					gender:options.sex,
					avatar_url:options.headimgurl,
					type:2,
					provider:that.provider
				};
				my.login(data,(res)=>{
					if(res.code == 8888){
						my.set_storage('token',res.data.token);
						my.set_storage('token_type',res.data.token_type);
						that.my();
					}
				})
			}
			//#endif
		},
		onShow(){
			var that = this;
			that.my();
			
		},
		methods: {
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				try{
					var that = this;
					that.my();
				} catch (e) {
					mescroll.endErr();
				}
				mescroll.endSuccess();
			},
			my(){
				var that = this;
				my.my((res)=>{
					if(res.code==8888){
						that.my_info = res.data;
						if(that.my_info.status_num>99){
							that.my_info.status_num = '99';
						}
						if(that.my_info.cart_count>99){
							that.my_info.cart_count = '99';
						}
						if(that.my_info.status_num>0){
							uni.setTabBarBadge({
							  index: 3,
							  text:that.my_info.status_num
							})
						}else{
							uni.removeTabBarBadge({
							  index: 3
							})
						}
						if(that.my_info.cart_count>0){
							uni.setTabBarBadge({
							  index: 2,
							  text:that.my_info.cart_count
							})
						}else{
							uni.removeTabBarBadge({
							  index: 2
							})
						}
					}
				})
			},
			wx_login(){
				var that = this;
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: function (loginRes) {
						var code = loginRes.code;
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								my.login({
									encryptData:infoRes.encryptedData,
									iv:infoRes.iv,
									code:code,
									type:2,
									provider:that.provider
								},(res)=>{
									if(res.code == 8888){
										my.set_storage('token',res.data.token);
										my.set_storage('token_type',res.data.token_type);
										that.my();
									}
								})
							}
						})
					}
				});
				//#endif
				// #ifdef APP-PLUS
				uni.login({
					provider: 'weixin',
					success: function (loginRes) {
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								var data = {
									gzh_open_id:infoRes.userInfo.openId,
									nick_name:infoRes.userInfo.nickName,
									gender:infoRes.userInfo.gender,
									avatar_url:infoRes.userInfo.avatarUrl,
									type:2,
									provider:that.provider
								};
								if(infoRes.userInfo.unionId){
									data.union_id=infoRes.userInfo.unionId;
								}
								my.login(data,(res)=>{
									if(res.code == 8888){
										my.set_storage('token',res.data.token);
										my.set_storage('token_type',res.data.token_type);
										that.my();
									}
								})
							}
						})
					}
				});	
				//#endif
				// #ifdef H5
				window.location.href = my.base_wx_login_url;
				//#endif
			},
			login(){
				my.navigate_to('../login/login');
			},
			//我的收藏
			my_collect(){
				var that = this;
				if(that.my_info.status!=1){
					my.show_tips(that,{title:'请登录！'});return;
				}else{
					my.navigate_to('../my_collect/my_collect');
				};			
			},
			//地址管理
			site_list(){
				var that = this;
				if(that.my_info.status!=1){
					my.show_tips({title:'请登录！'});return;
				}else{
					my.navigate_to('../location/location');
				};	
			},
			//设置
			setting(){
				var that = this;
				if(that.my_info.status!=1){
					my.show_tips({title:'请登录！'});return;
				}else{
					my.navigate_to('../setting/setting');
				};	
			},
			//我的钱包
			recharge_list(){
				var that = this;
				if(that.my_info.status!=1){
					my.show_tips({title:'请登录！'});return;
				}else{
					my.navigate_to('../recharge_list/recharge_list');
				};	
			},
			//关于我们
			config_content(){
				my.navigate_to('../config_content/config_content');
			},
			//跳转订单管理
			order(e){
				var that = this;
				if(that.my_info.status!=1){
					my.show_tips({title:'请登录！'});return;
				}
				var index = my.get_data_set(e,'index');
				my.navigate_to('../order/order?index='+index);
			}
		}
	}
</script>
<style>
	@import url("my.css");
</style>
